package com.apes.fn.scm.retailOrder.service.tc.tcOutbox;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.jpa.tuple.date.DateTime;
import com.apes.framework.util.DateUtil;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.GenerationTime;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Date;

/**
 * 功能：提成
 *
 * @author linwz
 * @create 2020-10-20 16:00
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = TcOutboxBackup.CONST_TABLE_NAME)
public class TcOutboxBackup extends Model {
	public static final String CONST_TABLE_NAME = "tc_outbox_backup";

	@Id
	@Column(name="id")
	@ApesGenerator(code = CONST_TABLE_NAME, name = "id")
	private Integer id;

	@Column(name = "aggregate_id", length = 64)
	@Comment("聚合根ID")
	private String aggregateId; //受给定事件影响的聚合根的id

	@Column(name = "aggregate_type", length = 64)
	@Comment("聚合根类型")
	private String aggregateType; //与给定事件相关的聚合根的类型

	@Column(name = "timestamp", length = 128)
	@Comment("时间戳")
	private Long timestamp; //时间戳

	@Lob
	@Basic(fetch = FetchType.LAZY)
	@Column(name = "payload", columnDefinition = "CLOB")
	@Comment("消息体")
	private String payload;

	@JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
	@DateTime(GenerationTime.INSERT)
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "create_date", updatable = false)
	@Comment("创建时间")
	private Date createDate;

	@JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
	@DateTime
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "update_date", insertable = false)
	@Comment("修改日期")
	private Date updateDate;

	@JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
	@DateTime
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "outbox_date", insertable = false)
	@Comment("下次消费日期")
	private Date outboxDate;

	@Column(name = "state")
	@Comment("状态：0-未消费；1-报错；2-已消费")
	private Integer state = 0;

	@Lob
	@Basic(fetch = FetchType.LAZY)
	@Column(name = "error", columnDefinition = "CLOB")
	@Comment("错误消息")
	private String error;

	@Column(name = "times")
	@Comment("消费次数")
	private int times = 0; //消费次数

	public void incTimes() {
		this.times += 1;
	}

}
